<template>
  <el-scrollbar>
    <div>
      <div style="margin-bottom: 5px; margin-top: 5px;">
        <el-input v-model="username" placeholder="请输入名字" :suffix-icon="SearchIcon" style="width: 300px; margin-left: 4px;"
        @keyup.enter.native="getData"></el-input>

    <el-select v-model="sex"
      placeholder="请选择性别" style="width: 300px; margin-left: 4px;"  @keyup.enter.native="getData"
       filterable>
       <el-option
            v-for="item in sexs"
            :key="item.value"
            :label="item.label"
            :value="item.value"></el-option>
       </el-select>

      <el-button type="primary" style="margin-left: 10px;" @click="getData">查询</el-button>
      <el-button type="danger" @click="resetParam">重置</el-button>
      <el-button type="success" @click="add">新增</el-button>
    </div>
          <el-table :data="tableData" 
          :header-cell-style="{background: '#B0C4DE', color:'#555','text-align': 'center'}"
                :cell-style="{padding:17+'px','text-align': 'center'}" 
                :border-color="black"
                style="margin-top:15px; font-size: 15px">
            <el-table-column prop="id" label="ID" width="80" />
            <el-table-column prop="username" label="昵称" width="165" />
            <el-table-column prop="account" label="账号" width="150"  />
            <el-table-column prop="sex" label="性别" width="100" >
              <template #default="scope">
        <el-tag
          :type="scope.row.sex === '男' ? 'primary' : 'danger'"
          disable-transitions>{{ scope.row.sex }}</el-tag>
      </template>
</el-table-column>
<el-table-column prop="roleid" label="角色" width="115"  >
  <template #default="scope">
        <el-tag
          :type="scope.row.roleid === 2 ? 'primary' : 'danger'"
          disable-transitions>{{ scope.row.roleid === 1 ? '管理员':'用户' }}</el-tag>
      </template>
  </el-table-column>
            <el-table-column prop="address" label="地址" width="250" />
            <el-table-column prop="tel" label="电话" width="200" />
            <el-table-column prop="operate" label="操作" >
              <template #default="scope">
              <el-button style="margin-left: 8px" type="success" @click="mod(scope.row)">编辑</el-button>

              <el-popconfirm title="确定要删除吗?" @confirm="del(scope.row.id)">
    <template #reference>
      <el-button style="margin-left: 8px" type="danger" >删除</el-button>
    </template>
  </el-popconfirm>
            </template>
              </el-table-column>
          </el-table>
          <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[2, 5, 10, 15]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"/>

      <el-dialog v-model="centerDialogVisible" title="提示" width="30%" center>
    <span>
      <el-form :rules="rules" :model="form" ref="form" label-width="120px">
        <el-form-item label="账号" prop="account">
      <el-input v-model="form.account" />
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="form.password" />
    </el-form-item>
    <el-form-item label="昵称" prop="username">
      <el-input v-model="form.username" />
    </el-form-item>
    <el-form-item label="性别">
      <el-radio-group v-model="form.sex">
        <el-radio label="男" />
        <el-radio label="女" />
      </el-radio-group>
    </el-form-item>
    <el-form-item label="电话" prop="tel">
      <el-input v-model="form.tel" />
    </el-form-item>
    <el-form-item label="地址">
      <el-input v-model="form.address" />
    </el-form-item>
    </el-form>
    </span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
        </div>
        </el-scrollbar>
</template>

<script>
import axios from 'axios';
import { Search } from '@element-plus/icons-vue'
export default {
  data() {
    let checkDuplicate =(rule,value,callback)=>{
                if(this.form.id){
                    return callback();
                }
                axios.get("/user/findByAccount?account="+this.form.account).then(res=>{
                    if(res.data.code!=200){
                        callback()
                    }else{
                        callback(new Error('账号已经存在'));
                    }
                })
            };
    return {
      tableData:[],
      pageSize: 10,
      pageNum: 1,
      total: 0,
      SearchIcon:Search,
      username:'',
      sex:'',
      sexs:[
        {
          value:'男',
          label:'男'
        },
        {
          value:'女',
          label:'女'
        }
      ],
      centerDialogVisible:false,
      form:{
        account:'',
        password:'',
        username:'',
        sex:'女',
        tel:'',
        address:'',
        //id:''
      },
      rules: {
        account: [
                    {required: true, message: '请输入账号', trigger: 'blur'},
                    {min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur'},
                    {validator:checkDuplicate,trigger: 'blur'}
        ],
        password: [
                      {required: true, message: '请输入密码', trigger: 'blur'},
                      {min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur'}
                    ],
        username: [
                      {required: true, message: '请输入昵称', trigger: 'blur'}
                    ],
        tel: [
                      {required: true,message: "手机号不能为空",trigger: "blur"},
                      {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur"}
                    ]
      }
    }
  },
  created(){
    this.getData();
  },
  methods:{
    getData() {
      axios.post('/user/listPageR',{
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        param: {
            username:this.username,
            sex:this.sex
        }
      }).then(response=> { 
        console.log(response.data)
        if(response.data.code == 200){
          this.tableData = response.data.data;
          this.total = response.data.total
        }else{
          alert('获取数据失败')
        }
      }).catch(function (error) {
        console.log(error);
      });
    },
    
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum = 1
      this.pageSize = val
      this.getData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val
      this.getData();
    },
    resetParam(){
      this.username='',
      this.sex=''
    },
    add(){
      this.centerDialogVisible = true;
      this.$nextTick(()=>{
        this.resetForm();
        this.form.id = ''
      })
    },
    doSave(){
      axios.post('/user/save',this.form).then(response=> { 
        console.log(response.data)
        if(response.data.code == 200){
         this.$message({
              message:'新增成功！',
              type: 'success'
         });
         this.centerDialogVisible = false;
         this.getData();
        }else{
          this.$message({
              message:'新增失败，请重试！',
              type: 'error'
         });
        }
      })
    },
    doMod(){
      axios.post('/user/update',this.form).then(response=> { 
        console.log(response.data)
        if(response.data.code == 200){
         this.$message({
              message:'修改成功！',
              type: 'success'
         });
         this.centerDialogVisible = false;
         this.getData();
        }else{
          this.$message({
              message:'修改失败，请重试！',
              type: 'error'
         });
        }
      })
    },
    save(){
      this.$refs.form.validate((valid) => {
                    if (valid) {
                      if(this.form.id) {
                        this.doMod();
                      }else{
                        this.doSave();
                      }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });   
    },
    resetForm(){
      this.$refs.form.resetFields();
    },
    mod(row){
      console.log(row);
      this.centerDialogVisible = true;
      this.form = row;
    },
    del(id){
      //console.log(id);
      axios.get('/orderlist/del?id='+id).then(response=> { 
        console.log(response.data)
        if(response.data.code == 200){
         this.$message({
              message:'删除成功！',
              type: 'success'
         });
         this.getData();
        }else{
          this.$message({
              message:'删除失败，请重试！',
              type: 'error'
         });
        }
      })
    }
  }
}
  
</script>